﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Security.Cryptography;
using System.IO;

namespace CYTS.Aoyou.Framework.Utility
{
    /// <summary>
    /// 加密
    /// </summary>
    public class TRIP3DESEncrypt
    {
        //密钥
        private const string sKey = "qJzGEh6hESZDVJeCnFPGuxzaiB7NLQM3";
        //矢量，矢量可以为空
        private const string sIV = "qcDY6X+aPLw=";
        //构造一个对称算法
        private SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
        /// <summary>
        /// 构造函数
        /// </summary>
        public TRIP3DESEncrypt() { }
        /// <summary>
        /// 对字符串进行3DSE加密
        /// </summary>
        /// <param name="Value">输入的字符串</param>
        /// <returns>加密后的字符串</returns>
        public string EncryptString(string Value)
        {
            ICryptoTransform ct = null;  //定义基本的加密转换运算。
            MemoryStream ms = null;  //创建其支持存储区为内存的流。
            CryptoStream cs = null; //定义将数据流链接到加密转换的流。
            byte[] byt = null;
            string StrResult = null; //最终结果
            try
            {
                mCSP.Key = Convert.FromBase64String(sKey);
                mCSP.IV = Convert.FromBase64String(sIV);
                //指定加密的运算模式
                mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
                //获取或设置加密算法的填充模式
                mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
                ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
                byt = Encoding.UTF8.GetBytes(Value);
                ms = new MemoryStream();
                cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
                cs.Write(byt, 0, byt.Length);
                cs.FlushFinalBlock();
                cs.Close();
                StrResult = Convert.ToBase64String(ms.ToArray());
            }
            catch (Exception e)
            {
                throw new Exception("对字符串进行3DSE加密失败", e);
            }
            finally
            {
                //关闭数据流操作
                if (ms != null)
                {
                    try
                    {
                        ms.Close();
                        ms = null;
                    }
                    catch (Exception e)
                    {
                        //捕捉关闭流时的异常
                        ms = null;
                        throw e;
                    }
                }
                if (cs != null)
                {
                    try
                    {
                        cs.Close();
                        cs = null;
                    }
                    catch (Exception e)
                    {
                        //捕捉关闭流时的异常
                        ms = null;
                        throw e;
                    }
                }
            }
            return StrResult;
        }
        /// <summary>
        /// 对字符串进行3DSE解密操作
        /// </summary>
        /// <param name="Value">加过密的字符串</param>
        /// <returns>解密后的字符串</returns>
        public string DecryptString(string Value)
        {
            ICryptoTransform ct = null;  //定义基本的加密转换运算。
            MemoryStream ms = null;  //创建其支持存储区为内存的流。
            CryptoStream cs = null; //定义将数据流链接到加密转换的流。
            byte[] byt = null;
            string StrResult = null; //最终结果
            try
            {
                mCSP.Key = Convert.FromBase64String(sKey);
                mCSP.IV = Convert.FromBase64String(sIV);
                mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
                mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
                ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
                byt = Convert.FromBase64String(Value);
                ms = new MemoryStream();
                cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
                cs.Write(byt, 0, byt.Length);
                cs.FlushFinalBlock();
                cs.Close();
                StrResult = Encoding.UTF8.GetString(ms.ToArray());
            }
            catch (Exception e)
            {
                throw new Exception("对字符串进行3DSE解密操作失败", e);
            }
            finally
            {
                //关闭数据流操作
                if (ms != null)
                {
                    try
                    {
                        ms.Close();
                        ms = null;
                    }
                    catch (Exception e)
                    {
                        //捕捉关闭流时的异常
                        ms = null;
                        throw e;
                    }
                }
                if (cs != null)
                {
                    try
                    {
                        cs.Close();
                        cs = null;
                    }
                    catch (Exception e)
                    {
                        //捕捉关闭流时的异常
                        ms = null;
                        throw e;
                    }
                }
            }
            return StrResult;
        }
    }
}
